package com.wmm.service.impl;

import com.wmm.base.StatusCode;
import com.wmm.dao.CourseContentDao;
import com.wmm.dao.impl.CourseContentDaoImpl;
import com.wmm.pojo.Course;
import com.wmm.pojo.Course_Lesson;
import com.wmm.pojo.Course_Section;
import com.wmm.service.CourseContentService;
import com.wmm.utils.DateUtils;

import java.util.List;

public class CourseContentServiceImpl implements CourseContentService {
    CourseContentDao contentDao = new CourseContentDaoImpl();
    @Override
    public List<Course_Section> findSectionAndLessonByCourseId(int courseId) {
        return contentDao.findSectionAndLessonByCourseId(courseId);
    }

    @Override
    public Course findCourseByCourseId(int courseId) {
        return contentDao.findCourseByCourseId(courseId);
    }

    @Override
    public String saveSection(Course_Section section) {
        //补全章节的信息
        section.setStatus(2) ;  //0 ：隐藏，1：待更新，2：已发布
        String date = DateUtils.getDateFormart();
        section.setCreate_time(date);
        section.setUpdate_time(date);
        //调用dao
        int row = contentDao.saveSection(section);

        //3.根据是否插入成功，封装对应的信息
        if(row > 0) {
            //保存成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        } else {
            //保存失败
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    @Override
    public String updateSection(Course_Section section) {
        //1.补全信息
        String date = DateUtils.getDateFormart();
        section.setUpdate_time(date);
        
        //2.调用dao
        int row = contentDao.updateSection(section);

        //3.判断是否插入成功
        if(row > 0) {
            String result = StatusCode.SUCCESS.toString();
            return result;
        } else {
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    @Override
    public String updateSectionStatus(int id, int status) {
        int i = contentDao.updateSectionStatus(id, status);
        if(i > 0) {
            return StatusCode.SUCCESS.toString();
        } else {
            return StatusCode.FAIL.toString();
        }
    }

    @Override
    public String saveLesson(Course_Lesson lesson) {
        int i = contentDao.saveLesson(lesson);
        if(i > 0) {
            return StatusCode.SUCCESS.toString();
        } else {
            return StatusCode.FAIL.toString();
        }
    }

    @Override
    public String updateLesson(Course_Lesson lesson) {

        int i = contentDao.updateLesson(lesson);
        if(i > 0) {
            return StatusCode.SUCCESS.toString();
        } else {
            return StatusCode.FAIL.toString();
        }
    }
}
